const md5 = require('md5');
const rp = require('request-promise');
const crypto = require('crypto');

module.exports = class extends think.Service {
  async create(orderSn, aiModel) {
    let aiToken = await think.cache('aiToken');
    if (!aiToken) {
      const TokenSerivce = await think.service('index', 'admin').create();
      aiToken = TokenSerivce.accessToken;
      await think.cache('aiToken', TokenSerivce.accessToken, {
        timeout: 10 * 60 * 1000,
      });
    }

    console.log({
      orderTokens: [orderSn],
      aiModelToken: aiModel,
      expireTime: 0,
    });

    try {
      // 获取 session
      const options = {
        method: 'POST',
        url: 'https://fbm-api.fbmms.cn/standard/b/aiInterview',
        headers: {
          'x-access-token': aiToken,
        },
        json: true,
        body: {
          orderTokens: [orderSn],
          aiModelToken: aiModel,
          expireTime: 0,
        },
      };

      const sessionData = await rp(options);
      return sessionData;
    } catch (e) {
      return null;
    }
  }
};
